package com.one.common.receiver;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.one.common.model.extra.BaseExtra;
import com.one.common.presenter.BasePresenter;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;


/**
 * creator: zhulunjun
 * time:    2017/11/17
 * describe: 中转页面的跳转逻辑
 * 默认跳转首页
 */

public class TransferPresenter extends BasePresenter {
    public TransferPresenter(Context mContext) {
        super(mContext);
    }

    public static final String ACTION = "actionAndroid"; // 需要跳转的页面
    public static final String PARAM1 = "param1"; // 第一个参数
    private static final String PARAM2 = "param2"; // 第二个参数
    private static final String PARAM3 = "param3"; // 第三个参数

    private String action, param1, param2, param3; // 获取的数据

    /**
     * 主界面
     */
    private static final String MAIN = "MainActivity";

    /**
     * 货源详情
     */
    private static final String GOODS_DETAIL = "GoodsDetailActivity";

    /**
     * 订单详情
     */
    private static final String ORDER_DETAIL = "OrderDetailActivity";

    /**
     * owtb订单详情
     */
    private static final String ORDER_DETAIL_OWTB = "OWTBOrderDetailActivity";

    /**
     * 上传回单
     */
    private static final String UPLOAD_RECEIPT = "UploadReceiptActivity";

    @Override
    public void onCreate() {
        super.onCreate();
        distributePage(mActivity.getIntent());
    }

    public void getIntent() {

    }

    /**
     * 分发页面
     */
    public void distributePage(Intent intent) {
        Logger.d("外部跳转");
        if (Intent.ACTION_VIEW.equals(intent.getAction()) && intent.getData() != null) {
            Uri uri = intent.getData();
            action = uri.getQueryParameter(ACTION);
            param1 = uri.getQueryParameter(PARAM1);
            param2 = uri.getQueryParameter(PARAM2);
            param3 = uri.getQueryParameter(PARAM3);
            Logger.d("外部跳转 " + action);
            if (StringUtils.isNotBlank(action)) {
//                // 先处理跳转主页的uri, 当前在主页。如果有下标，则返回
//                if (action.contains(MAIN)) {
//                    if (StringUtils.isNotBlank(param1)) {
//                        BusManager.getBus().post(new IndexEvent(StringUtils.getIntToString(param1)));
//                    }
//                } else if (action.contains(GOODS_DETAIL)) {
//                    // 货源详情
//                    if (StringUtils.isNotBlank(param1) && StringUtils.isNotBlank(param2)) {
//                        RouterManager.getInstance().go(RouterPath.GOODS_DETAIL,
//                                new OrderDetailExtra(param2, param1));
//                    }
//                } else if (action.contains(ORDER_DETAIL_OWTB)) {
//                    //owtb订单详情
//                    if (StringUtils.isNotBlank(param1)) {
//                        if (CMemoryData.isCar()) {
//                            RouterManager.getInstance().go(RouterPath.OWTB_ORDER_DETAIL, new DefaultExtra(param1, 1));
//                        } else {
//                            RouterManager.getInstance().go(RouterPath.OWTB_ORDER_DETAIL, new DefaultExtra(param1));
//                        }
//                    }
//                } else if (action.contains(ORDER_DETAIL)) {
//                    //订单详情
//                    if (StringUtils.isNotBlank(param1)) {
//                        RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new OrderDetailExtra(param1, ""));
//                    }
//                } else if(action.contains(UPLOAD_RECEIPT)){
//                    // 回单被驳回，上传回单
//                    if(StringUtils.isNotBlank(param1)){
//                        ReceiptExtra extra = new ReceiptExtra(param1,ReceiptExtra.SHOW);
//                        RouterManager.getInstance().go(RouterPath.UPLOAD_RECEIPT, extra);
//                    }
//
//                } else {
//                    jumpAction(action);
//                }

            }

        }

    }

    /**
     * 通过action 跳转页面
     *
     * @param action    包名路径
     * @param extra     参数
     * @param extraName 参数key
     */
    private void jumpAction(String action, BaseExtra extra, String extraName) {
        Class<?> clazz = null;
        try {
            Intent intent = new Intent();
            clazz = Class.forName(action);
            intent.setClass(mContext, clazz);
            if (extra != null) {
                intent.putExtra(extraName, extra);
            }
            mContext.startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void jumpAction(String action) {
        jumpAction(action, null, null);
    }


}
